
#include "lib_sdl2.h"




static SDL_Window*   window     = NULL;
static SDL_Renderer *renderer   = NULL;
static SDL_Texture * texture    = NULL;



int sdl2_create_win(void)
{
	int ret = -1;

	int win_w = 1088;
	int win_h = 1920;

	ret = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
    if(ret < 0){
		log_e("sdl init error:%s\n",SDL_GetError());
		return -1;
    }

    window = SDL_CreateWindow("rkplay",
                              SDL_WINDOWPOS_UNDEFINED,       // 默认x y 显示到窗口中间
                              SDL_WINDOWPOS_UNDEFINED,
                              win_w,
                              win_h,
                              SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);// 设置窗口属性
    if(!window){
        log_e("Can't create window, err:%s\n", SDL_GetError());
        return -1;
    }

	renderer = SDL_CreateRenderer(window, -1, 0);
    if(NULL == renderer)
    {
        printf("Create Renderer error: %s\n", SDL_GetError());
        // 销毁窗口，释放资源
        SDL_DestroyWindow(window);
        SDL_Quit();
        return -1;
    }
 
    // 基于渲染器创建纹理
    texture = SDL_CreateTexture(renderer,
                                SDL_PIXELFORMAT_NV12,
                                SDL_TEXTUREACCESS_STREAMING,
                              	win_w,win_h);
	
	if(NULL == texture)
	{
		SDL_DestroyRenderer(renderer);
		SDL_DestroyWindow(window);
		SDL_Quit();
		return -1;
	}
	
}



int sdl2_destory_win(void)
{
	if(window){
		SDL_DestroyWindow(window);
	}

	SDL_Quit(); 					


	return 0;
}

int sdl2_write_file(const char* buff,int buff_size, const char* filename)
{
	int fd = -1;
	int ret = -1;

	ret = remove(filename);
	fd = open(filename, O_CREAT|O_WRONLY);
	if(fd == (-1)){
		log_e("open file %s error\n",filename);
		return ;
	}

	write(fd,buff,buff_size);

	close(fd);
	fd = -1;

	return 0;
}


int sdl2_render_show(MppFrame frame )
{
	int w = 0;
	int h = 0;
	int err= -1;
	int size = 0;
	
	SDL_Rect rect;
	uint8_t* data = NULL;

	
	log_i("\n");

    err = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame);
    if (err) {
        log_e("get err info %d discard %d, go back.\n",
                mpp_frame_get_errinfo(frame),
                mpp_frame_get_discard(frame));
        return -1;
    }
	log_i("\n");
    w    = mpp_frame_get_width(frame);
    h    = mpp_frame_get_height(frame);
	data = mpp_frame_get_buffer(frame);
	size = mpp_buffer_get_size(mpp_frame_get_buffer(frame));

	rect.x = 0;
	rect.y = 0;
	rect.w = w;
	rect.h = h;

	
	sdl2_write_file(data,size,"./test.data");

	log_i("w=%d\n",w);	
	log_i("h=%d\n",h);	
	log_i("size=%d\n",size);

	//SDL_SetRenderTarget(renderer, texture);
	//log_i("\n");

	err = SDL_UpdateTexture(texture, NULL, data, 1088);
    if(err < 0){
		log_e("update text error:%s\n",SDL_GetError());
		return -1;
    }
	

	SDL_RenderCopy(renderer, texture, NULL, NULL);
	SDL_RenderPresent(renderer);
	log_i("\n");
	


	return 0;
}

int sdl2_play_nv12_video(void)
{
    SDL_Window* window = NULL;          // 窗口
    SDL_Renderer* renderer = NULL;      // 渲染器
    SDL_Texture* texture = NULL;        // 纹理

	int win_w = 768;
	int win_h = 432;

	
    SDL_Init(SDL_INIT_VIDEO);
 
    // 创建窗口
    window = SDL_CreateWindow("Window Title",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              win_w,
                              win_h,
                              SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if(NULL == window)
    {
        printf("Create Window error: %s\n", SDL_GetError());
        return -1;
    }
 
    // 创建渲染器(一个窗口可有多个渲染器)
    renderer = SDL_CreateRenderer(window,-1, 0);
    if(NULL == renderer)
    {
        printf("Create Renderer error: %s\n", SDL_GetError());
        // 销毁窗口，释放资源
        SDL_DestroyWindow(window);
        SDL_Quit();
        return -1;
    }
 
    // 基于渲染器创建纹理
    texture = SDL_CreateTexture(renderer,
                                SDL_PIXELFORMAT_NV12,// SDL_PIXELFORMAT_IYUV
								SDL_TEXTUREACCESS_STREAMING,
                                win_w,
                                win_h);
    if(NULL == texture)
    {
        printf("Create Texture error: %s\n", SDL_GetError());
        // 销毁渲染器 窗口 释放资源
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return -1;
    }

 	//FILE *pFile = fopen("./out240x128.yuv", "rb");
	FILE* pFile = fopen("./768x432_nv12.yuv", "rb");

	int frameSize = win_w * win_h * 3 / 2;

	unsigned char* yuv_data = (unsigned char*)malloc(frameSize);

	int read_cnt = 0;

    while(1)
    {

		if(fread(yuv_data,1,frameSize,pFile) != frameSize)
		{
			fseek(pFile,0,SEEK_SET);
			fread(yuv_data,1,frameSize,pFile);
			break;
		}

		sdl2_write_file(yuv_data,frameSize,"./test_nv12.data");


		SDL_UpdateTexture(texture, NULL,yuv_data,win_w);
		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer,texture, NULL, NULL);
		SDL_RenderPresent(renderer);



		SDL_Delay(33);

		read_cnt++;
		if(read_cnt >= 1)
		{
			break;
		}

	}
 
    // 销毁 纹理 渲染器 窗口，释放资源
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
 
    return 0;
}






















